package cn.edu.zjgsu.ito.controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.edu.zjgsu.ito.service.AdminSingleStuScoreService;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import cn.edu.zjgsu.ito.service.StudentExportPdfService;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;

public class AdminSingleStuScoreController extends Controller {
	/**
	 * 单个学生成绩初始化
	 * 杨昆鹏
	 */
	AdminSingleStuScoreService adminSingleStuScoreService = ServiceFactory.createAdminSingleStuScoreService();
	StudentExportPdfService studentExportPdfService = ServiceFactory.createStudentExportPdfService();
	
	
	public void index()
	{	
		String passport = getPara("passportNo");
		System.out.println(passport);
		List<Record> stuGrade = adminSingleStuScoreService.stuGrade(passport);
		
		System.out.println(stuGrade.size());
		System.out.println(stuGrade);
		/*for(int i = 0;i < stuGrade.size();i++){
			System.out.println(stuGrade.get(i).getStr("termValue"));
			System.out.println(stuGrade.get(i).getStr("courseNo"));
			System.out.println(stuGrade.get(i).getStr("courseName"));
			System.out.println(stuGrade.get(i).getInt("credit"));
			System.out.println(stuGrade.get(i).getInt("gradeStatus"));
			System.out.println(stuGrade.get(i).getStr("grade"));
			System.out.println(stuGrade.get(i).getStr("gradeLevel"));
		}*/
		setAttr("passportNo",passport);
		setAttr("stuGrade",stuGrade);
		render("studentscore.html");
	}
	
	/**
	 * 单个学生成绩打印页面
	 */
	public void print()
	{	
		//跳转页面
		String passportNo = getPara("passportNo");
		setAttr("passportNo",passportNo);
		render("scoreprint.html");
	}
	
	//给打印页面数据
	public void dataPrint(){
		String passportNo = getPara("passportNo");
		ArrayList<List<Record>> reAllStuGras = (ArrayList<List<Record>>) adminSingleStuScoreService.dataPrint(passportNo);
		renderJson(reAllStuGras);
	}
	
	//打印学生成绩单
	public void printStuGraPdf(){

		String passportNo = getPara("passportNo");
		
		OutputStream out = null;
		Date date = new Date();
		DateFormat formatter=new SimpleDateFormat("yyyyMMddhhmmss");
		String time=formatter.format(date);
		String filedir = PathKit.getWebRootPath();
		filedir = filedir + "\\WEB-INF\\download\\";
		System.out.println("------1------");
        deleteFile(new File(filedir));//清空缓存区
		String temp = passportNo + "成绩" + time +".pdf";
		String outpath = filedir + temp;
		
		try {
			System.out.println(outpath);
			out = new FileOutputStream(outpath);
			studentExportPdfService.createPdf(out,passportNo);
			String newOutPath = filedir + passportNo + "成绩单" + time +".pdf";
			studentExportPdfService.addWater(outpath,newOutPath);
			renderFile(new File(newOutPath));
			System.out.println("结束");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	 		
	}
	
	public void deleteFile(File oldPath) {
        if (oldPath.isDirectory()) {
         File[] files = oldPath.listFiles();
         for (File file : files) {
           deleteFile(file);
         }
        }else{
          oldPath.delete();
        }
	}

}
